﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Buddy.Samples
{
    static partial class Program
    {
        static void Samples_Defer()
        {
            try
            {
                using (var defer = new Defer())
                {
                    var res = AllocateResource();
                    defer.Also += () => DeallocateResource(res);

                    Process();
                }
            }
            catch { Console.WriteLine("BANG!"); }

            Console.WriteLine("press any key to continue ...");
            Console.ReadKey();
        }

        static string AllocateResource()
        {
            Console.WriteLine("-> resource allocated");
            return "aloha";
        }

        static void DeallocateResource(string res) { Console.WriteLine("<- resource de-allocated"); }

        static void Process() { throw new InvalidOperationException(); }
    }
}
